package com.lingkang.flychat.ui.activity.main;

import com.lingkang.flychat.dto.ResponseResult;
import com.lingkang.flychat.entity.ContactsFriend;
import com.lingkang.flychat.entity.ContactsFriendChilds;
import com.lingkang.flychat.entity.ContactsGroup;
import com.lingkang.flychat.entity.FriendList;
import com.lingkang.flychat.http.HttpUtils;
import com.lingkang.flychat.http.ResponseListenter;
import com.lingkang.flychat.http.service.UserService;
import com.orhanobut.hawk.Hawk;

import java.util.ArrayList;
import java.util.List;

public class MainActivityInit {

    public static List<ContactsGroup> groups = new ArrayList<>();
    public static List<ContactsFriendChilds> childs = new ArrayList<>();
    public static List<FriendList> friendLists = new ArrayList<>();

    public static ContactsFriend getContactsFriendByUsername(String username) {
        for (ContactsFriendChilds c : childs) {
            for (ContactsFriend friend : c.getContactsFriends()) {
                if (friend.getUsername().equals(username))
                    return friend;
            }
        }
        return null;
    }

    /**
     * 初始好友列表
     */
    public static void initFriend() {
        HttpUtils.request(HttpUtils.createApi(UserService.class).friendList(), new ResponseListenter<ResponseResult<List<FriendList>>>() {
            @Override
            public void onSuccess(ResponseResult<List<FriendList>> data) {
                if (data.getData() == null)
                    return;
                friendLists = data.getData();
                for (FriendList list : friendLists) {
                    boolean has = false;
                    for (ContactsGroup group : groups) {
                        if (group.getGroupId() == list.getGroupId()) {
                            has = true;
                            break;
                        }
                    }
                    if (!has) { //去重
                        ContactsGroup group = new ContactsGroup();
                        group.setGroupId(list.getGroupId());
                        group.setGroupName(list.getGroupName());
                        groups.add(group);
                    }
                }

                for (ContactsGroup g : groups) {
                    List<ContactsFriend> friends = new ArrayList<>();
                    for (FriendList list : friendLists) {
                        if (g.getGroupId() == list.getGroupId()) {
                            ContactsFriend friend = new ContactsFriend();
                            friend.setUserId(list.getUserId());
                            friend.setImg(list.getImg());
                            friend.setAutograph(list.getAutograph());
                            friend.setUsername(list.getUsername());
                            friend.setName(list.getName());
                            friends.add(friend);
                        }
                    }
                    ContactsFriendChilds friendChilds = new ContactsFriendChilds();
                    friendChilds.setGroupId(g.getGroupId());
                    friendChilds.setContactsFriends(friends);
                    childs.add(friendChilds);
                }
                Hawk.put("initFriendList", true);
            }

            @Override
            public void onFail(String data) {

            }
        });
    }
}
